<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Calculating an Integral</title>
<link rel="stylesheet" href="../../../../multiprecision.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Chapter 1. Boost.Multiprecision">
<link rel="up" href="../fp_eg.html" title="Examples">
<link rel="prev" href="nd.html" title="Calculating a Derivative">
<link rel="next" href="poly_eg.html" title="Polynomial Evaluation">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="nd.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fp_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="poly_eg.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="boost_multiprecision.tut.floats.fp_eg.gi"></a><a class="link" href="gi.html" title="Calculating an Integral">Calculating
          an Integral</a>
</h5></div></div></div>
<p>
            Similar to the generic derivative example, we can calculate integrals
            in a similar manner:
          </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">value_type</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">function_type</span><span class="special">&gt;</span>
<span class="keyword">inline</span> <span class="identifier">value_type</span> <span class="identifier">integral</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">a</span><span class="special">,</span>
                           <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">b</span><span class="special">,</span>
                           <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">tol</span><span class="special">,</span>
                           <span class="identifier">function_type</span> <span class="identifier">func</span><span class="special">)</span>
<span class="special">{</span>
   <span class="keyword">unsigned</span> <span class="identifier">n</span> <span class="special">=</span> <span class="number">1U</span><span class="special">;</span>

   <span class="identifier">value_type</span> <span class="identifier">h</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">b</span> <span class="special">-</span> <span class="identifier">a</span><span class="special">);</span>
   <span class="identifier">value_type</span> <span class="identifier">I</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">func</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">func</span><span class="special">(</span><span class="identifier">b</span><span class="special">))</span> <span class="special">*</span> <span class="special">(</span><span class="identifier">h</span> <span class="special">/</span> <span class="number">2</span><span class="special">);</span>

   <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">k</span> <span class="special">=</span> <span class="number">0U</span><span class="special">;</span> <span class="identifier">k</span> <span class="special">&lt;</span> <span class="number">8U</span><span class="special">;</span> <span class="identifier">k</span><span class="special">++)</span>
   <span class="special">{</span>
      <span class="identifier">h</span> <span class="special">/=</span> <span class="number">2</span><span class="special">;</span>

      <span class="identifier">value_type</span> <span class="identifier">sum</span><span class="special">(</span><span class="number">0</span><span class="special">);</span>
      <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">1U</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;=</span> <span class="identifier">n</span><span class="special">;</span> <span class="identifier">j</span><span class="special">++)</span>
      <span class="special">{</span>
         <span class="identifier">sum</span> <span class="special">+=</span> <span class="identifier">func</span><span class="special">(</span><span class="identifier">a</span> <span class="special">+</span> <span class="special">(</span><span class="identifier">value_type</span><span class="special">((</span><span class="identifier">j</span> <span class="special">*</span> <span class="number">2</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">)</span> <span class="special">*</span> <span class="identifier">h</span><span class="special">));</span>
      <span class="special">}</span>

      <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">I0</span> <span class="special">=</span> <span class="identifier">I</span><span class="special">;</span>
      <span class="identifier">I</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">I</span> <span class="special">/</span> <span class="number">2</span><span class="special">)</span> <span class="special">+</span> <span class="special">(</span><span class="identifier">h</span> <span class="special">*</span> <span class="identifier">sum</span><span class="special">);</span>

      <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">ratio</span>     <span class="special">=</span> <span class="identifier">I0</span> <span class="special">/</span> <span class="identifier">I</span><span class="special">;</span>
      <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">delta</span>     <span class="special">=</span> <span class="identifier">ratio</span> <span class="special">-</span> <span class="number">1</span><span class="special">;</span>
      <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">delta_abs</span> <span class="special">=</span> <span class="special">((</span><span class="identifier">delta</span> <span class="special">&lt;</span> <span class="number">0</span><span class="special">)</span> <span class="special">?</span> <span class="special">-</span><span class="identifier">delta</span> <span class="special">:</span> <span class="identifier">delta</span><span class="special">);</span>

      <span class="keyword">if</span><span class="special">((</span><span class="identifier">k</span> <span class="special">&gt;</span> <span class="number">1U</span><span class="special">)</span> <span class="special">&amp;&amp;</span> <span class="special">(</span><span class="identifier">delta_abs</span> <span class="special">&lt;</span> <span class="identifier">tol</span><span class="special">))</span>
      <span class="special">{</span>
         <span class="keyword">break</span><span class="special">;</span>
      <span class="special">}</span>

      <span class="identifier">n</span> <span class="special">*=</span> <span class="number">2U</span><span class="special">;</span>
   <span class="special">}</span>

   <span class="keyword">return</span> <span class="identifier">I</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
            The following sample program shows how the function can be called, we
            begin by defining a function object, which when integrated should yield
            the Bessel J function:
          </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">value_type</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">cyl_bessel_j_integral_rep</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
   <span class="identifier">cyl_bessel_j_integral_rep</span><span class="special">(</span><span class="keyword">const</span> <span class="keyword">unsigned</span> <span class="identifier">N</span><span class="special">,</span>
      <span class="keyword">const</span> <span class="identifier">value_type</span><span class="special">&amp;</span> <span class="identifier">X</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">n</span><span class="special">(</span><span class="identifier">N</span><span class="special">),</span> <span class="identifier">x</span><span class="special">(</span><span class="identifier">X</span><span class="special">)</span> <span class="special">{</span> <span class="special">}</span>

   <span class="identifier">value_type</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="identifier">value_type</span><span class="special">&amp;</span> <span class="identifier">t</span><span class="special">)</span> <span class="keyword">const</span>
   <span class="special">{</span>
      <span class="comment">// pi * Jn(x) = Int_0^pi [cos(x * sin(t) - n*t) dt]</span>
      <span class="keyword">return</span> <span class="identifier">cos</span><span class="special">(</span><span class="identifier">x</span> <span class="special">*</span> <span class="identifier">sin</span><span class="special">(</span><span class="identifier">t</span><span class="special">)</span> <span class="special">-</span> <span class="special">(</span><span class="identifier">n</span> <span class="special">*</span> <span class="identifier">t</span><span class="special">));</span>
   <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
   <span class="keyword">const</span> <span class="keyword">unsigned</span> <span class="identifier">n</span><span class="special">;</span>
   <span class="keyword">const</span> <span class="identifier">value_type</span> <span class="identifier">x</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<pre class="programlisting">   <span class="comment">/* The function can now be called as follows: */</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">**)</span>
<span class="special">{</span>
   <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">constants</span><span class="special">::</span><span class="identifier">pi</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_dec_float_50</span> <span class="identifier">mp_type</span><span class="special">;</span>

   <span class="keyword">const</span> <span class="keyword">float</span> <span class="identifier">j2_f</span> <span class="special">=</span>
      <span class="identifier">integral</span><span class="special">(</span><span class="number">0.0F</span><span class="special">,</span>
      <span class="identifier">pi</span><span class="special">&lt;</span><span class="keyword">float</span><span class="special">&gt;(),</span>
      <span class="number">0.01F</span><span class="special">,</span>
      <span class="identifier">cyl_bessel_j_integral_rep</span><span class="special">&lt;</span><span class="keyword">float</span><span class="special">&gt;(</span><span class="number">2U</span><span class="special">,</span> <span class="number">1.23F</span><span class="special">))</span> <span class="special">/</span> <span class="identifier">pi</span><span class="special">&lt;</span><span class="keyword">float</span><span class="special">&gt;();</span>

   <span class="keyword">const</span> <span class="keyword">double</span> <span class="identifier">j2_d</span> <span class="special">=</span>
      <span class="identifier">integral</span><span class="special">(</span><span class="number">0.0</span><span class="special">,</span>
      <span class="identifier">pi</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;(),</span>
      <span class="number">0.0001</span><span class="special">,</span>
      <span class="identifier">cyl_bessel_j_integral_rep</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;(</span><span class="number">2U</span><span class="special">,</span> <span class="number">1.23</span><span class="special">))</span> <span class="special">/</span> <span class="identifier">pi</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;();</span>

   <span class="keyword">const</span> <span class="identifier">mp_type</span> <span class="identifier">j2_mp</span> <span class="special">=</span>
      <span class="identifier">integral</span><span class="special">(</span><span class="identifier">mp_type</span><span class="special">(</span><span class="number">0</span><span class="special">),</span>
      <span class="identifier">pi</span><span class="special">&lt;</span><span class="identifier">mp_type</span><span class="special">&gt;(),</span>
      <span class="identifier">mp_type</span><span class="special">(</span><span class="number">1.0E-20</span><span class="special">),</span>
      <span class="identifier">cyl_bessel_j_integral_rep</span><span class="special">&lt;</span><span class="identifier">mp_type</span><span class="special">&gt;(</span><span class="number">2U</span><span class="special">,</span> <span class="identifier">mp_type</span><span class="special">(</span><span class="number">123</span><span class="special">)</span> <span class="special">/</span> <span class="number">100</span><span class="special">))</span> <span class="special">/</span> <span class="identifier">pi</span><span class="special">&lt;</span><span class="identifier">mp_type</span><span class="special">&gt;();</span>

   <span class="comment">// 0.166369</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="keyword">float</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">j2_f</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>

   <span class="comment">// 0.166369383786814</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">j2_d</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>

   <span class="comment">// 0.16636938378681407351267852431513159437103348245333</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">mp_type</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">j2_mp</span>
      <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>

   <span class="comment">//</span>
   <span class="comment">// Print true value for comparison:</span>
   <span class="comment">// 0.166369383786814073512678524315131594371033482453329</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="identifier">mp_type</span><span class="special">(</span><span class="number">123</span><span class="special">)</span> <span class="special">/</span> <span class="number">100</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John
      Maddock and Christopher Kormanyos<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="nd.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fp_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="poly_eg.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
